package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.BrowseActivity;
import com.github.postsanf.yinian.activity.CardStatusCommentActivity;
import com.github.postsanf.yinian.activity.CardStatusDetailActivity;
import com.github.postsanf.yinian.activity.MemoryCardDetailActivity;
import com.github.postsanf.yinian.activity.PicWallShareActivity;
import com.github.postsanf.yinian.activity.SharePhotoActivity;
import com.github.postsanf.yinian.activity.StatusShareActivity;
import com.github.postsanf.yinian.activity.TimeKeyWordActivity;
import com.github.postsanf.yinian.activity.TransferEventActivity;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNInviteSecret;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.NoScrollListview;
import com.github.postsanf.yinian.view.StyleDialog;
import com.github.postsanf.yinian.widget.RotateAnimation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

public class CardAlbumStatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<YNStatus> mDatas;
    private OnItemClickListener mListener;
    private boolean isInAlbum = false;
    private static final int EMPTY_VIEW = 10;
    private HashMap<Integer, String> datemap;
    private HashMap<Integer, Boolean> yearmap;
    int nowyytime = 0;
    int nowyear = 0;
    private boolean enableRefresh;
    private int userid;
    private int groupid;

    public boolean isInAlbum() {
        return isInAlbum;
    }

    public void setIsInAlbum(boolean isInAlbum) {
        this.isInAlbum = isInAlbum;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public CardAlbumStatusAdapter(Context context, List<YNStatus> mDatas, int adapterPage, int userid, int groupid) {
        this.context = context;
        this.mDatas = mDatas;
        this.userid = userid;
        this.groupid = groupid;
        datemap = new HashMap<>();
        yearmap = new HashMap<>();
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout item_memorycard_background;
        LinearLayout ll_look_history;
        LinearLayout ll_status;
        View line1;
        View line2;
        LinearLayout vitem;
        LinearLayout view_time;
        TextView view_time_mouth;
        TextView view_time_year;
        SimpleDraweeView item_avatar;
        TextView card_item_name;
        TextView card_item_time;

        GridView mGridView;
        ImageView item_singepic;
        ImageView iv_day_sign;

        ImageView item_memorycardpic;
        TextView item_memorylocation;
        RelativeLayout item_memorycard;

        RelativeLayout item_timecard_reversal;
        LinearLayout item_timecard_reversal_cover;
        ImageView item_timecardpic;
        ImageView item_timecard_share;
        ImageView item_timecard_transfer;
        ImageView item_timecard_comment;

        LinearLayout item_time_card_reversal_back;
        ImageView item_timecardpic_back;
        ImageView item_timecard_mic_iv;
        TextView item_timecard_mic_tx;
        TextView item_timecard_from_content;

        TextView item_etext;
        TextView item_pic_more;
        View item_pic_more_line;
        ImageView card_item_share;
        ImageView card_item_transfer;
        ImageView card_item_comment;
        LinearLayout ll_card_lv_comments;
        NoScrollListview card_lv_comments;
        TextView card_lv_comments_more;

        ImageView view_time_whiteblack;
        ImageView view_time_whiteblack_small;

        public ViewHolder(View itemView) {
            super(itemView);
            mGridView = (GridView) itemView.findViewById(R.id.item_gridview);
            line1 = (View) itemView.findViewById(R.id.view_1);
            line2 = (View) itemView.findViewById(R.id.view_2);
            view_time_whiteblack = (ImageView) itemView.findViewById(R.id.view_time_whiteblack);
            view_time_whiteblack_small = (ImageView) itemView.findViewById(R.id.view_time_whiteblack_small);
            vitem = (LinearLayout) itemView.findViewById(R.id.item);
            ll_look_history = ((LinearLayout) itemView.findViewById(R.id.ll_look_history));
            item_avatar = (SimpleDraweeView) itemView.findViewById(R.id.card_item_avatar);
            card_item_name = (TextView) itemView.findViewById(R.id.card_item_name);
            card_item_time = (TextView) itemView.findViewById(R.id.card_item_time);
            item_singepic = (ImageView) itemView.findViewById(R.id.item_singepic);
            iv_day_sign = (ImageView) itemView.findViewById(R.id.iv_day_sign);
            item_pic_more = (TextView) itemView.findViewById(R.id.item_pic_more);
            item_pic_more_line = (View) itemView.findViewById(R.id.item_pic_more_line);
            item_etext = (TextView) itemView.findViewById(R.id.item_etext);
            ll_status = (LinearLayout) itemView.findViewById(R.id.ll_status);

            item_memorycard = (RelativeLayout) itemView.findViewById(R.id.item_memorycard);
            item_memorycard_background = ((LinearLayout) itemView.findViewById(R.id.item_memorycard_background));
            item_memorycardpic = (ImageView) itemView.findViewById(R.id.item_memorycardpic);
            item_memorylocation = (TextView) itemView.findViewById(R.id.item_memorylocation);

            item_timecard_reversal = (RelativeLayout) itemView.findViewById(R.id.item_timecard_reversal);
            item_timecard_reversal_cover = (LinearLayout) itemView.findViewById(R.id.item_timecard_reversal_cover);
            item_timecardpic = (ImageView) itemView.findViewById(R.id.item_timecardpic);
            item_timecard_share = (ImageView) itemView.findViewById(R.id.item_timecard_share);
            item_timecard_transfer = (ImageView) itemView.findViewById(R.id.item_timecard_transfer);
            item_timecard_comment = (ImageView) itemView.findViewById(R.id.item_timecard_comment);
            item_time_card_reversal_back = (LinearLayout) itemView.findViewById(R.id.item_time_card_reversal_back);
            item_timecardpic_back = (ImageView) itemView.findViewById(R.id.item_timecardpic_back);
            item_timecard_mic_iv = (ImageView) itemView.findViewById(R.id.item_timecard_mic_iv);
            item_timecard_mic_tx = (TextView) itemView.findViewById(R.id.item_timecard_mic_tx);
            item_timecard_from_content = (TextView) itemView.findViewById(R.id.item_timecard_from_content);

            card_item_share = (ImageView) itemView.findViewById(R.id.card_item_share);
            card_item_transfer = (ImageView) itemView.findViewById(R.id.card_item_transfer);
            card_item_comment = (ImageView) itemView.findViewById(R.id.card_item_comment);
            ll_card_lv_comments = (LinearLayout) itemView.findViewById(R.id.ll_card_lv_comments);
            card_lv_comments = (NoScrollListview) itemView.findViewById(R.id.card_lv_comments);
            card_lv_comments_more = (TextView) itemView.findViewById(R.id.card_lv_comments_more);
            view_time = (LinearLayout) itemView.findViewById(R.id.view_time);
            view_time_mouth = (TextView) itemView.findViewById(R.id.view_time_mouth);
            view_time_year = (TextView) itemView.findViewById(R.id.view_time_year);
        }

        public void bindData(final YNStatus item, final int position) {
            if (item != null) {
                if (position == 0) {
                    nowyytime = 0;
                    nowyear = 0;
                }
                String dateStr = item.getEuploadtime();
//                item_comment_num.setText(StringUtils.numberToStr((item.getComments() == null || item.getComments().size() == 0) ? 0 : item.getComments().size()));
//                tv_timecard_comment_number.setText(StringUtils.numberToStr((item.getComments() == null || item.getComments().size() == 0) ? 0 : item.getComments().size()));
                try {
                    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(dateStr);
                    String InYear = DateFormat.format("yyyy", date).toString();
                    String InMouth = DateFormat.format("MM", date).toString();
                    String Indd = DateFormat.format("dd", date).toString();
                    String dateItem = InMouth + "-" + Indd;
                    String dateItem2 = InYear + "." + InMouth;
                    datemap.put(position, dateItem);
                    if (position == 0) {
                        view_time_mouth.setText(Indd);
                        view_time_year.setText(dateItem2);
                    } else {
                        if (TextUtils.equals(dateItem, datemap.get(position - 1))) {
                            view_time_year.setVisibility(View.INVISIBLE);
                            view_time_mouth.setVisibility(View.INVISIBLE);
                        } else {
                            view_time_year.setVisibility(View.VISIBLE);
                            view_time_mouth.setVisibility(View.VISIBLE);
                            view_time_mouth.setText(Indd);
                            view_time_year.setText(dateItem2);
                        }
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String hms = dateStr.split(" ")[1];
                String Inhour = hms.split(":")[0] + ":" + hms.split(":")[1];
                card_item_time.setText(Inhour + " ");
//                final List<String> sslist = item.getUrl();
                final List<String> sslist = item.getThumbnail();
                List<String> sslist2 = getUrlList(sslist);
                String fristImg = YNUrlHelper.getStatusUrl(context, sslist.get(0));
                final String avatarPaths = YNUrlHelper.getAvatarUrl(context, item.getPublishUser().getUpic());
                ImageOptHelper.showFrescoAvatar(Uri.parse(avatarPaths), item_avatar, context);
                card_item_name.setText(item.getPublishUser().getUnickname());
                RelativeLayout.LayoutParams lineparams = (RelativeLayout.LayoutParams) line2.getLayoutParams();
                if (item.getEtype() == 1) {
                    //记录卡片
                    view_time_whiteblack.setImageResource(R.drawable.circle_yellow);
                    view_time_whiteblack_small.setImageResource(R.drawable.circle_yellow);
                    ll_status.setVisibility(View.VISIBLE);
                    item_memorycard.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getHigh(context) + UIHelper.dip2px(context, 25)));
                    vitem.setVisibility(View.VISIBLE);
                    mGridView.setVisibility(View.GONE);
                    item_pic_more.setVisibility(View.GONE);
                    item_pic_more_line.setVisibility(View.GONE);
                    item_etext.setVisibility(View.GONE);
                    item_singepic.setVisibility(View.GONE);
                    item_timecard_reversal.setVisibility(View.GONE);
                    item_memorycard.setVisibility(View.VISIBLE);
                    item_memorycard_background.setVisibility(View.VISIBLE);
                    ImageOptHelper.showFrescoStatus(sslist.get(0), item_memorycardpic, context);
                    if (!StringUtils.isEmpty(item.getEplace())) {
                        if (item.getEplace().length() > 8)
                            item_memorylocation.setText(item.getEplace().substring(0, 7) + "...");
                        else
                            item_memorylocation.setText(item.getEplace());
                    } else {
                        item_memorylocation.setText("忆年");
                    }
                    int lvcomm = 0;
                    if (item.getComments() != null && item.getComments().size() > 0) {
                        ll_card_lv_comments.setVisibility(View.VISIBLE);
                        YNSingleton.getInstance().setCurStatus(item);
                        CardCommentAdapter mAdapter = new CardCommentAdapter(context, item.getComments(), userid, groupid);
                        card_lv_comments.setAdapter(mAdapter);
                        if (item.getComments().size() > 10) {
                            card_lv_comments_more.setVisibility(View.VISIBLE);
                            card_lv_comments_more.setText("更多评论 " + (item.getComments().size() - 10) + " >>");
                            card_lv_comments_more.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    YNSingleton.getInstance().setCurStatus(item);
                                    context.startActivity(new Intent(context, CardStatusCommentActivity.class).putExtra("comment", true));
                                }
                            });
                        } else {
                            card_lv_comments_more.setVisibility(View.INVISIBLE);
                        }
                        int totalHeight = 0;
                        for (int i = 0; i < mAdapter.getCount(); i++) {
                            View listItem = mAdapter.getView(i, null, card_lv_comments);
                            listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//                            listItem.measure(0, 0);
                            totalHeight += listItem.getMeasuredHeight();
                        }
                        lvcomm = UIHelper.dip2px(context, 50) + totalHeight;
                    } else {
                        lvcomm = 0;
                        ll_card_lv_comments.setVisibility(View.GONE);
                    }
                    lineparams.height = lvcomm + UIHelper.dip2px(context, 64) + UIHelper.getHigh(context) + UIHelper.dip2px(context, 75) - UIHelper.dip2px(context, 36);
                    line2.setLayoutParams(lineparams);
                    ll_look_history.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
                            context.startActivity(new Intent(context, MemoryCardDetailActivity.class).putExtra("lookStatus", "lookStatus"));
                            context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        }
                    });
                    item_memorycard.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            YNSingleton.getInstance().setCurStatus(item);
                            context.startActivity(new Intent(context, MemoryCardDetailActivity.class).putExtra("lookStatus", "lookStatus"));
                            context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        }
                    });
                    ll_status.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
                            context.startActivity(new Intent(context, MemoryCardDetailActivity.class).putExtra("lookStatus", "lookStatus"));
                            context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        }
                    });
                } else if (item.getEtype() == 2) {
                    view_time_whiteblack.setImageResource(R.drawable.circle_dark_blue);
                    view_time_whiteblack_small.setImageResource(R.drawable.circle_dark_blue);
                    ll_status.setVisibility(View.VISIBLE);
                    item_timecard_reversal.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getTimeCardHigh(context)));
                    vitem.setVisibility(View.GONE);
                    item_memorycard.setVisibility(View.GONE);
                    item_pic_more.setVisibility(View.GONE);
                    item_timecard_reversal.setVisibility(View.VISIBLE);
//                    ImageOptHelper.showFrescoSingeTimeCardCover(Uri.parse(item.getEcover()), item_timecardpic, context);
//                    ImageOptHelper.showFrescoSingeTimeCardCover(Uri.parse(StringUtils.repaceImgUrl(fristImg)), item_timecardpic_back, context);
                    ImageOptHelper.showFrescoStatus(item.getEcover() + "?imageView2/2/w/480", item_timecardpic, context);
                    ImageOptHelper.showFrescoStatus(StringUtils.repaceImgUrl(fristImg), item_timecardpic_back, context);
                    if (StringUtils.isEmpty(item.getEaudio())) {
                        item_timecard_mic_tx.setText("0'00");
                        item_timecard_mic_iv.setImageResource(R.drawable.timecard_sound_no);
                    } else {
                        item_timecard_mic_tx.setText(" ");
                        item_timecard_mic_iv.setImageResource(R.drawable.timecard_detail_mic3);
                    }
                    if (!StringUtils.isEmpty(item.getEplace())) {
                        item_timecard_from_content.setText(item.getEplace());
                    }
                    int lvcomm = 0;
                    if (item.getComments() != null && item.getComments().size() > 0) {
                        ll_card_lv_comments.setVisibility(View.VISIBLE);
                        YNSingleton.getInstance().setCurStatus(item);
                        CardCommentAdapter mAdapter = new CardCommentAdapter(context, item.getComments(), userid, groupid);
                        card_lv_comments.setAdapter(mAdapter);
                        if (item.getComments().size() > 10) {
                            card_lv_comments_more.setVisibility(View.VISIBLE);
                            card_lv_comments_more.setText("更多评论 " + (item.getComments().size() - 10) + " >>");
                            card_lv_comments_more.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    YNSingleton.getInstance().setCurStatus(item);
                                    context.startActivity(new Intent(context, CardStatusCommentActivity.class).putExtra("comment", true));
                                }
                            });
                        } else {
                            card_lv_comments_more.setVisibility(View.INVISIBLE);
                        }
                        int totalHeight = 0;
                        for (int i = 0; i < mAdapter.getCount(); i++) {
                            View listItem = mAdapter.getView(i, null, card_lv_comments);
                            listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                            totalHeight += listItem.getMeasuredHeight();
                        }
                        lvcomm = UIHelper.dip2px(context, 50) + totalHeight;
                    } else {
                        lvcomm = 0;
                        ll_card_lv_comments.setVisibility(View.GONE);
                    }
                    lineparams.height = lvcomm + UIHelper.dip2px(context, 65) + UIHelper.getTimeCardHigh(context) - UIHelper.dip2px(context, 36);
                    line2.setLayoutParams(lineparams);
                    item_timecard_reversal.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            timeCardAnimation(item, position);
                        }
                    });
                    ll_look_history.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
//                            context.startActivity(new Intent(context, TimeCardDetailActivity.class).putExtra("lookLimeCard", "lookLimeCard"));
                            timeCardAnimation(item, position);
                        }
                    });
                    ll_status.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
//                            context.startActivity(new Intent(context, TimeCardDetailActivity.class).putExtra("lookLimeCard", "lookLimeCard"));
                            timeCardAnimation(item, position);
                        }
                    });
                } else if (item.getEtype() == 3) {
                    view_time_whiteblack.setImageResource(R.drawable.circle_blue);
                    view_time_whiteblack_small.setImageResource(R.drawable.circle_blue);
                    int lvcomm = 0;
                    if (item.getComments() != null && item.getComments().size() > 0) {
                        ll_card_lv_comments.setVisibility(View.VISIBLE);
                        YNSingleton.getInstance().setCurStatus(item);
                        CardCommentAdapter mAdapter = new CardCommentAdapter(context, item.getComments(), userid, groupid);
                        card_lv_comments.setAdapter(mAdapter);
                        if (item.getComments().size() > 10) {
                            card_lv_comments_more.setVisibility(View.VISIBLE);
                            card_lv_comments_more.setText("更多评论 " + (item.getComments().size() - 10) + " >>");
                            card_lv_comments_more.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    YNSingleton.getInstance().setCurStatus(item);
                                    context.startActivity(new Intent(context, CardStatusCommentActivity.class).putExtra("comment", true));
                                }
                            });
                        } else {
                            card_lv_comments_more.setVisibility(View.INVISIBLE);
                        }
                        int totalHeight = 0;
                        for (int i = 0; i < mAdapter.getCount(); i++) {
                            View listItem = mAdapter.getView(i, null, card_lv_comments);
                            listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                            totalHeight += listItem.getMeasuredHeight();
                        }
                        lvcomm = UIHelper.dip2px(context, 52) + totalHeight;
                    } else {
                        lvcomm = 0;
                        ll_card_lv_comments.setVisibility(View.GONE);
                    }
                    iv_day_sign.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getDaySignHight(context)));
                    lineparams.height = lvcomm + UIHelper.getDaySignHight(context) + UIHelper.dip2px(context, 63);
                    line2.setLayoutParams(lineparams);
                    ll_status.setVisibility(View.VISIBLE);
                    item_memorycard.setVisibility(View.GONE);
                    item_timecard_reversal.setVisibility(View.GONE);
                    vitem.setVisibility(View.VISIBLE);
                    mGridView.setVisibility(View.GONE);
                    item_pic_more.setVisibility(View.GONE);
                    item_pic_more_line.setVisibility(View.GONE);
                    item_singepic.setVisibility(View.GONE);
                    item_etext.setVisibility(View.GONE);
                    iv_day_sign.setVisibility(View.VISIBLE);
                    ImageOptHelper.showFrescoStatus(item.getUrl().get(0), iv_day_sign, context);
                    iv_day_sign.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
                            Intent dsIntent = new Intent(context, BrowseActivity.class);
                            dsIntent.putExtra(CommonConstants.YNPOSITION, 0);
                            dsIntent.putExtra("isDaySign", true);
                            dsIntent.putExtra("type", "time");
                            context.startActivity(dsIntent);
                        }
                    });
                } else {
                    view_time_whiteblack.setImageResource(R.drawable.circle_black);
                    view_time_whiteblack_small.setImageResource(R.drawable.circle_black);
                    ll_status.setVisibility(View.VISIBLE);
                    item_memorycard.setVisibility(View.GONE);
                    item_timecard_reversal.setVisibility(View.GONE);
                    vitem.setVisibility(View.VISIBLE);
                    iv_day_sign.setVisibility(View.GONE);
                    if (item.getEtext() != null && !item.getEtext().equals("")) {
                        item_etext.setVisibility(View.VISIBLE);
                        item_etext.setText(item.getEtext());
                    } else {
                        item_etext.setVisibility(View.GONE);
                        item_etext.setText("");
                    }
                    int lvcomm = 0;
                    if (item.getComments() != null && item.getComments().size() > 0) {
                        ll_card_lv_comments.setVisibility(View.VISIBLE);
                        YNSingleton.getInstance().setCurStatus(item);
                        CardCommentAdapter mAdapter = new CardCommentAdapter(context, item.getComments(), userid, groupid);
                        card_lv_comments.setAdapter(mAdapter);
                        if (item.getComments().size() > 10) {
                            card_lv_comments_more.setVisibility(View.VISIBLE);
                            card_lv_comments_more.setText("更多评论 " + (item.getComments().size() - 10) + " >>");
                            card_lv_comments_more.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    YNSingleton.getInstance().setCurStatus(item);
                                    context.startActivity(new Intent(context, CardStatusDetailActivity.class).putExtra("comment", true));
                                    context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                                }
                            });
                        } else {
                            card_lv_comments_more.setVisibility(View.INVISIBLE);
                        }
                        int totalHeight = 0;
                        for (int i = 0; i < mAdapter.getCount(); i++) {
                            View listItem = mAdapter.getView(i, null, card_lv_comments);
                            listItem.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                            totalHeight += listItem.getMeasuredHeight();
//                            Log.i("xjs", "listItem.getMeasuredHeight()===》" + listItem.getMeasuredHeight());
                        }
                        lvcomm = UIHelper.dip2px(context, 50) + totalHeight;
                    } else {
                        lvcomm = 0;
                        ll_card_lv_comments.setVisibility(View.GONE);
                    }
                    if (sslist.size() == 1) {
                        if (item.getEtext() != null && !item.getEtext().equals("")) {
                            lineparams.height = lvcomm + UIHelper.dip2px(context, 110) + UIHelper.getsingeHigh(context) + UIHelper.dip2px(context, 60) - UIHelper.dip2px(context, 36);
                        } else {
                            lineparams.height = lvcomm + UIHelper.dip2px(context, 70) + UIHelper.getsingeHigh(context) + UIHelper.dip2px(context, 60) - UIHelper.dip2px(context, 36);
                        }
                        line2.setLayoutParams(lineparams);
                        item_memorycard.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIHelper.getsingeHigh(context)));
                        mGridView.setVisibility(View.GONE);
                        item_pic_more.setVisibility(View.GONE);
                        item_pic_more_line.setVisibility(View.GONE);
                        item_singepic.setVisibility(View.VISIBLE);
//                        ImageOptHelper.showFrescoSingePic(Uri.parse(StringUtils.repaceImgUrl(fristImg)), item_singepic, context);
                        ImageOptHelper.showFrescoStatus(item.getUrl().get(0), item_singepic, context);
                        item_singepic.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                YNSingleton.getInstance().setCurStatus(item);
                                Intent intent = new Intent(context, BrowseActivity.class);
                                intent.putExtra(CommonConstants.YNPOSITION, 0);
                                intent.putExtra("type", "time");
                                context.startActivity(intent);
                            }
                        });
                    } else {
                        if (item.getEtext() != null && !item.getEtext().equals("")) {
                            lineparams.height = lvcomm + UIHelper.dip2px(context, 64) + UIHelper.getHigh(context) + UIHelper.dip2px(context, 80) - UIHelper.dip2px(context, 36);
                        } else {
                            lineparams.height = lvcomm + UIHelper.dip2px(context, 33) + UIHelper.getHigh(context) + UIHelper.dip2px(context, 80) - UIHelper.dip2px(context, 36);
                        }
                        item_singepic.setVisibility(View.GONE);
                        mGridView.setVisibility(View.VISIBLE);
                        mGridView.setAdapter(new CardGridViewAdapter(context, sslist2, sslist.size()));
                        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                YNSingleton.getInstance().setCurStatus(item);
                                Intent intent = new Intent(context, BrowseActivity.class);
                                intent.putExtra(CommonConstants.YNPOSITION, position);
                                intent.putExtra("type", "time");
                                context.startActivity(intent);
                            }
                        });
                        if (sslist.size() <= 3) {
                            item_pic_more.setVisibility(View.GONE);
                            item_pic_more_line.setVisibility(View.GONE);
                        } else if (sslist.size() > 3) {
                            item_pic_more.setVisibility(View.VISIBLE);
                            item_pic_more_line.setVisibility(View.GONE);
                            item_pic_more.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    YNSingleton.getInstance().setCurStatus(item);
                                    context.startActivity(new Intent(context, CardStatusDetailActivity.class));
                                    context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                                }
                            });
                        }
                        line2.setLayoutParams(lineparams);
                    }
                    ll_look_history.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
                            context.startActivity(new Intent(context, CardStatusDetailActivity.class));
                            context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        }
                    });
                    ll_status.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            YNSingleton.getInstance().setCurStatus(item);
                            context.startActivity(new Intent(context, CardStatusDetailActivity.class));
                            context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        }
                    });
                }
                item_timecard_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        YNSingleton.getInstance().setCurStatus(item);
                        Intent intent = new Intent(context, TimeKeyWordActivity.class);
                        context.startActivity(intent);
                    }
                });
                card_item_comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        YNSingleton.getInstance().setCurStatus(item);
                        Intent intent = new Intent(context, TimeKeyWordActivity.class);
                        context.startActivity(intent);
                    }
                });
                item_timecard_transfer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        YNSingleton.getInstance().setCurStatus(item);
                        context.startActivity(new Intent(context, TransferEventActivity.class).putExtra("type", 2));
                        Intent statintenttransfer = new Intent(ACTIONs.record);
                        statintenttransfer.putExtra("key", "同步_4");
                        statintenttransfer.putExtra("value", 1);
                        context.sendBroadcast(statintenttransfer);
                    }
                });
                card_item_transfer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        YNSingleton.getInstance().setCurStatus(item);
                        if (item.getEtype() == 1) {
                            context.startActivity(new Intent(context, TransferEventActivity.class).putExtra("type", 1));
                        } else if (item.getEtype() == 3) {
                            context.startActivity(new Intent(context, TransferEventActivity.class).putExtra("type", 3));
                        } else {
                            context.startActivity(new Intent(context, TransferEventActivity.class).putExtra("type", 0));
                        }
                        Intent statintenttransfer = new Intent(ACTIONs.record);
                        statintenttransfer.putExtra("key", "同步_4");
                        statintenttransfer.putExtra("value", 1);
                        context.sendBroadcast(statintenttransfer);
                    }
                });
//                ll_status_comment.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (item.getEtype() == 1) {
//                            YNSingleton.getInstance().setCurStatus(item);
//                            context.startActivity(new Intent(context, MemoryCardDetailCommentActivity.class));
//                        } else {
//                            YNSingleton.getInstance().setCurStatus(item);
//                            context.startActivity(new Intent(context, StatusDetailActivity.class));
//                        }
//                    }
//                });
                //时光明信片评论按钮
//                item_timecard_comment.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        YNSingleton.getInstance().setCurStatus(item);
//                        context.startActivity(new Intent(context, MemoryCardDetailCommentActivity.class).putExtra("timeCard", "timeCard"));
//                    }
//                });
                //时光明信片分享
                item_timecard_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.getEtype() == 2) {
                            StyleDialog styleDialog = new StyleDialog(context);
                            styleDialog.setCanceledOnTouchOutside(true);
//                            boolean timeLineLead = SpUtils.getBoolean(context, "TimeLineLead");
//                            Log.d("yuhao", "timeLineLead=" + timeLineLead);
//                            if (!timeLineLead) {
//                                context.sendBroadcast(new Intent(ACTIONs.lead));
//                            } else {
                            styleDialog.show();
//                            }
                            styleDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialog) {
                                    SharedPreferences sp = context.getSharedPreferences("isApart", Context.MODE_PRIVATE);
                                    int isApart = sp.getInt("isApart", 0);
                                    if (isApart == 1) {
                                        Intent intent = new Intent(context, SharePhotoActivity.class);
                                        intent.putStringArrayListExtra(ACTIONs.aAddData, (ArrayList<String>) item.getUrl());
                                        intent.putExtra("type", "item_timecard_share");
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                    } else if (isApart == 2) {
                                        String url = context.getResources().getString(R.string.SHARE_CARD);
                                        String sec = null;
                                        try {
                                            sec = YNInviteSecret.encryptDES("eventid=" + item.getEid(), context.getResources().getString(R.string.UNKNOW_ID));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        url = url + sec;
                                        Intent intent = new Intent(context, StatusShareActivity.class);
                                        intent.putExtra("share", "share");
                                        intent.putExtra("type", "item_timecard_share");
                                        intent.putExtra("avatarPaths", avatarPaths);
                                        intent.putExtra("fristpic", sslist.get(0));
                                        intent.putExtra("title", item.getPublishUser().getUnickname() + " 有点想你，并给你寄了一张语音图集！");
                                        intent.putExtra("desc", "这是我分享给你的风景，以及想对你说的话");
                                        intent.putExtra("url", url);
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                    } else if (isApart == 3) {
                                        String url = context.getResources().getString(R.string.SHARE_CARD);
                                        String sec = null;
                                        try {
                                            sec = YNInviteSecret.encryptDES("eventid=" + item.getEid(), context.getResources().getString(R.string.UNKNOW_ID));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        url = url + sec;
                                        Intent intent = new Intent(context, PicWallShareActivity.class);
                                        intent.putExtra("QRcode", "QRcode");
                                        intent.putExtra("UrlShare", url);
                                        intent.putExtra("firstPic", sslist.get(0));
                                        intent.putExtra("nickName", item.getPublishUser().getUnickname() + "和你分享了一套ta制作的语音图集");
                                        intent.putExtra("QRtext", "长按识别二维码查看全部照片");
                                        intent.putExtra("above", "语音图集：可以录制自己声音的动态放映的照片集");
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                    }
                                }
                            });
                            Intent statintentshare = new Intent(ACTIONs.record);
                            statintentshare.putExtra("key", "分享（语音图集）_4");
                            statintentshare.putExtra("value", 1);
                            context.sendBroadcast(statintentshare);
                        }
                    }
                });
                card_item_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.getEtype() == 1) {
                            StyleDialog styleDialog = new StyleDialog(context);
                            styleDialog.setCanceledOnTouchOutside(true);
//                            boolean timeLineLead = SpUtils.getBoolean(context, "TimeLineLead");
//                            if (!timeLineLead) {
//                                context.sendBroadcast(new Intent(ACTIONs.lead));
//                            } else {
                            styleDialog.show();
//                            }
                            styleDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialog) {
                                    SharedPreferences sp = context.getSharedPreferences("isApart", Context.MODE_PRIVATE);
                                    int isApart = sp.getInt("isApart", 0);
                                    if (isApart == 1) {
                                        Intent intent = new Intent(context, SharePhotoActivity.class);
                                        intent.putStringArrayListExtra(ACTIONs.aAddData, (ArrayList<String>) item.getUrl());
                                        intent.putExtra("type", "card_item_share");
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                        Intent statintentshare1 = new Intent(ACTIONs.record);
                                        statintentshare1.putExtra("key", "分享（记录卡片·部分图片）_4");
                                        statintentshare1.putExtra("value", 1);
                                        context.sendBroadcast(statintentshare1);
                                    } else if (isApart == 2) {
                                        String url = context.getResources().getString(R.string.SHARE_JIYI);
                                        String sec = null;
                                        try {
                                            sec = YNInviteSecret.encryptDES("eventid=" + item.getEid(), context.getResources().getString(R.string.UNKNOW_ID));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        url = url + sec;
                                        Intent intent = new Intent(context, StatusShareActivity.class);
                                        intent.putExtra("share", "share");
                                        intent.putExtra("type", "card_item_share");
                                        intent.putExtra("avatarPaths", avatarPaths);
                                        intent.putExtra("fristpic", sslist.get(0));
                                        intent.putExtra("title", item.getPublishUser().getUnickname() + "。" + "有一段美好的时光@你！");
                                        intent.putExtra("desc", "秘密花园限时开放哦！");
                                        intent.putExtra("url", url);
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                        Intent statintentshare2 = new Intent(ACTIONs.record);
                                        statintentshare2.putExtra("key", "分享（记录卡片·二维码）_4");
                                        statintentshare2.putExtra("value", 1);
                                        context.sendBroadcast(statintentshare2);
                                    } else if (isApart == 3) {
                                        String url = context.getResources().getString(R.string.SHARE_JIYI);
                                        String sec = null;
                                        try {
                                            sec = YNInviteSecret.encryptDES("eventid=" + item.getEid(), context.getResources().getString(R.string.UNKNOW_ID));
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                        url = url + sec;
                                        Intent intent = new Intent(context, PicWallShareActivity.class);
                                        intent.putExtra("QRcode", "QRcode");
                                        intent.putExtra("UrlShare", url);
                                        intent.putExtra("firstPic", sslist.get(0));
                                        intent.putExtra("nickName", item.getPublishUser().getUnickname() + "和你分享了一张ta制作的记录卡片");
                                        intent.putExtra("QRtext", "长按识别二维码查看全部照片");
                                        intent.putExtra("above", "记录卡片：记录某个重要时刻完整细节的图文卡片");
                                        context.startActivity(intent);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putInt("isApart", 0);
                                        edit.commit();
                                        Intent statintentshare3 = new Intent(ACTIONs.record);
                                        statintentshare3.putExtra("key", "分享（记录卡片·整体）_4");
                                        statintentshare3.putExtra("value", 1);
                                        context.sendBroadcast(statintentshare3);
                                    }
                                }
                            });
                        } else if (item.getEtype() == 3) {
                            Intent intent = new Intent(context, StatusShareActivity.class);
                            intent.putExtra("share", "share");
                            intent.putExtra("avatarPaths", avatarPaths);
                            intent.putExtra("fristpic", item.getUrl().get(0));
                            intent.putExtra("type", "day_sign");
                            context.startActivity(intent);
                            Intent statintentbtnmenu = new Intent(ACTIONs.record);
                            statintentbtnmenu.putExtra("key", "分享（一日一记）_4");
                            statintentbtnmenu.putExtra("value", 1);
                            context.sendBroadcast(statintentbtnmenu);
                        } else {
                            Intent intent = new Intent(context, SharePhotoActivity.class);
                            intent.putStringArrayListExtra(ACTIONs.aAddData, (ArrayList<String>) item.getUrl());
                            context.startActivity(intent);
                            Intent statintentshare3 = new Intent(ACTIONs.record);
                            statintentshare3.putExtra("key", "分享（上传照片）_4");
                            statintentshare3.putExtra("value", 1);
                            context.sendBroadcast(statintentshare3);
                        }
                    }
                });
            }
        }

        private void timeCardAnimation(final YNStatus item, final int position) {
            enableRefresh = true;
            float cX = item_timecard_reversal.getWidth() / 2.0f;
            float cY = item_timecard_reversal.getHeight() / 2.0f;
            RotateAnimation rotateAnim = new RotateAnimation(cX, cY, RotateAnimation.ROTATE_INCREASE);
            if (rotateAnim != null) {
                rotateAnim.setInterpolatedTimeListener(new RotateAnimation.InterpolatedTimeListener() {
                    @Override
                    public void interpolatedTime(float interpolatedTime) {
                        if (enableRefresh && interpolatedTime > 0.5f) {
                            item_timecard_reversal_cover.setVisibility(View.GONE);
                            item_time_card_reversal_back.setVisibility(View.VISIBLE);
                            enableRefresh = false;
                        }
                    }
                });
                rotateAnim.setFillAfter(true);
                item_timecard_reversal.startAnimation(rotateAnim);
                rotateAnim.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        YNSingleton.getInstance().setCurStatus(item);
                        context.sendBroadcast(new Intent(ACTIONs.actionToTimeCardDetail));
                        context.sendBroadcast(new Intent(ACTIONs.actionLookStatus).putExtra("eid", mDatas.get(position).getEid()));
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                item_timecard_reversal_cover.setVisibility(View.VISIBLE);
                                item_time_card_reversal_back.setVisibility(View.GONE);
                            }
                        }, 1300);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
            }
        }
    }

    private List<String> getUrlList(List<String> sslist) {
        List<String> sslist2 = new ArrayList<>();
        sslist2.clear();
        if (sslist.size() >= 3) {
            for (int i = 0; i < 3; i++) {
                sslist2.add(sslist.get(i));
            }
        } else {
            sslist2 = sslist;
        }
        return sslist2;
    }

    private YNStatus getItem(int position) {
        return mDatas.get(position);
    }

    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            int resId = R.layout.yn_empty_status;
            View v = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
            v.getRootView().getLayoutParams().height = DisplayUtils.getScreenHeightPixels((Activity) context) - DisplayUtils.dp2px(context, 210);
            return new EmptyViewHolder(v);
        }
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_include_card_album_status_item, parent, false);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.OnItemClick(v, (YNStatus) convertView.getTag());
                }
            }
        });
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, final int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            holder.setIsRecyclable(false);
            final YNStatus item = getItem(position);
            holder.bindData(item, position);
            holder.itemView.setTag(item);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return mDatas.size() > 0 ? mDatas.size() : 1;
    }

    public void addItemsFromTop(List<YNStatus> items) {
        if (items == null)
            return;
        this.mDatas.addAll(0, items);
        this.notifyItemRangeInserted(0, items.size());
        this.notifyDataSetChanged();
    }

    public void addItemsFromTopOne(YNStatus items) {
        if (items == null)
            return;
        this.mDatas.add(0, items);
        this.notifyItemRangeInserted(0, 1);
        this.notifyDataSetChanged();
    }

    public void removeOne(int ids) {
        for (int i = 0; i < mDatas.size(); i++) {
            if (ids == mDatas.get(i).getEid()) {
                this.mDatas.remove(i);
                this.notifyItemRemoved(i);
                break;
            }
        }
    }

    public void SyncComment() {
        for (int i = 0; i < mDatas.size(); i++) {
            if (mDatas.get(i).getEid() == YNSingleton.getInstance().getCurStatus().getEid()) {
                mDatas.get(i).setComments(YNSingleton.getInstance().getCurStatus().getComments());
                CardAlbumStatusAdapter.this.notifyDataSetChanged();
            }
        }
    }

    public void addItemsFromBottom(List<YNStatus> items) {
        if (items == null)
            return;
        this.mDatas.addAll(mDatas.size(), items);
        this.notifyItemRangeInserted(mDatas.size(), items.size());
    }

    public interface OnItemClickListener {
        public void OnItemClick(View view, YNStatus data);
    }

    public int getIdByDirection(boolean isTop) {
        if (isTop) {
            if (mDatas.size() <= 0) {
                return 0;
            }
            return mDatas.get(0).getEid();
        } else {
            if (mDatas.size() <= 0) {
                return 0;
            }
            return mDatas.get(mDatas.size() - 1).getEid();
        }
    }
}
